package com.example.pingze.controller;

import com.example.pingze.entity.Manager;
import com.example.pingze.exception.PingZeException;
import com.example.pingze.repository.ManagerRepository;
import com.example.pingze.service.ManagerService;
import com.example.pingze.utils.ResultVOUtils;
import com.example.pingze.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/manager")
@CrossOrigin(origins = "*")
public class ManagerController {
    @Autowired
    private ManagerService service;
    @Autowired
    private ManagerRepository repository;
    @CrossOrigin
    @PostMapping("/login")
    public Result<Manager> login(String no, String password){
        Manager info = service.login(no,password);
        return ResultVOUtils.result(info);
    }
    @CrossOrigin
    @PostMapping("/superManagerLogin")
    public Result<Manager> superLogin(String no, String password){
        Manager info = repository.findManagerByNo(no);
        if(!no.equals("pingze")){
            throw new PingZeException("超管账号不正确");
        }
        if(!password.equals(info.getPassword())){
            throw new PingZeException("密码不正确");
        }
        return ResultVOUtils.result(info);
    }

    @CrossOrigin
    @PostMapping("/register")
    public Result<Manager> register(String no, String password){
        Manager add = service.register(no,password);
        return ResultVOUtils.result(add);
    }
    @CrossOrigin
    @PostMapping("/page")
    public Result<Page<Manager>> getList(
            Manager manager,
            @RequestParam(name="pageCode",required = false,defaultValue = "1") Integer pageCode,
            @RequestParam(name="pageSize",required = false, defaultValue = "4") Integer pageSize
    ){
        if(pageCode<1){
            throw new PingZeException("页码不能小于1");
        }
        Sort.Order noOrder = Sort.Order.asc("no");
        Sort sort = Sort.by(noOrder);
        Pageable pageable = PageRequest.of(pageCode-1,pageSize,sort);
        Page page = service.getManagerList(manager,pageable);
        return ResultVOUtils.result(page);
    }
    @CrossOrigin
    @PostMapping("/findManagerById")
    public Result<Manager> findManagerById(String id){
        Manager manager = repository.findManagerById(id);
        return ResultVOUtils.result(manager);
    }
    @CrossOrigin
    @PostMapping("/add")
    public Result<Manager> add(String no, String password,Integer sex){
        Manager add = service.add(no,password,sex);
        return ResultVOUtils.result(add);
    }

    @CrossOrigin
    @PostMapping("/edit")
    public Result<Manager> edit(String id,String no,String password,Integer sex,Integer roleId){
        Manager edit = service.edit(id,no, password,sex,roleId);
        return ResultVOUtils.result(edit);
    }

    @CrossOrigin
    @Transactional
    @PostMapping("/delete")
    public Result<String> delete(String id){
        repository.deleteManagerById(id);
        return new ResultVOUtils().success("delete successful");
    }
}
